#include "data_import_dialog.h"

DataImportDialog::DataImportDialog(QWidget *parent) : QDialog(parent){

    QLabel *headerLbl = new QLabel(tr("Below you can indicate which kinds of data are in your file.  The order of the data <b>must</b> as indicated (strata, spatial, external, loci)."));
    headerLbl->setWordWrap(true);

    QHBoxLayout *fileLayout = new QHBoxLayout();
    QLabel *fileLabel = new QLabel(tr("Data File to be Imported:"));
    fileLabel->setToolTip(tr("The file must be a tab-delimited text file"));
    QPushButton *fileButton = new QPushButton(tr("Find File"));
    connect( fileButton, SIGNAL(clicked()), this, SLOT(slotLoadFile()));
    fileLayout->addWidget(fileLabel);
    fileLayout->addWidget(fileButton);

    _pathLabel = new QLabel("<font color='red'>...</font>");


    QGridLayout *gridLayout = new QGridLayout();
    gridLayout->setColumnMinimumWidth(1,10);

    QLabel *strataLabel = new QLabel(tr("Number of Strata:"));
    strataLabel->setToolTip(tr("The number of grouping (categorical) columns of data to be imported."));
    _numStrataEdit = new NumberEdit();
    _numStrataEdit->setText("1");
    gridLayout->addWidget(strataLabel,0,0);
    gridLayout->addWidget(_numStrataEdit,0,2);

    QLabel *spatialLabel = new QLabel(tr("Number of Spatial Coordinates:"));
    spatialLabel->setToolTip(tr("The number of spatial (coordinate) columns of data to be imported"));
    _numSpatialEdit = new NumberEdit();
    _numSpatialEdit->setText("0");
    gridLayout->addWidget(spatialLabel,1,0);
    gridLayout->addWidget(_numSpatialEdit,1,2);

    QLabel *externalLabel = new QLabel(tr("Number of External Variables:"));
    externalLabel->setToolTip(tr("The number of external (continuous) columns of data to be imported"));
    _numExternalEdit = new NumberEdit();
    _numExternalEdit->setText("0");
    gridLayout->addWidget(externalLabel,2,0);
    gridLayout->addWidget(_numExternalEdit,2,2);

    QLabel *lociLabel = new QLabel(tr("Number of Loci:"));
    lociLabel->setToolTip(tr("The number of genetic loci to be imported"));
    _numLociEdit = new NumberEdit();
    _numLociEdit->setText("20");
    gridLayout->addWidget(lociLabel,3,0);
    gridLayout->addWidget(_numLociEdit,3,2);

    QGroupBox *groupBox = new QGroupBox(tr("File Contents"));
    groupBox->setLayout( gridLayout );

    _genotypeFormats = new QComboBox();
    QStringList formats;
    formats << QObject::tr("Haploid") << QObject::tr("Diploid One Column Format") << QObject::tr("Diploid Two Column Format") << QObject::tr("Tetraploid");
    _genotypeFormats->addItems( formats );
    _genotypeFormats->setCurrentIndex(1);
    QLabel *formatLabel = new QLabel(tr("Genetic Data Type:"));
    QHBoxLayout *formatLayout = new QHBoxLayout();
    formatLayout->addWidget( formatLabel );
    formatLayout->addWidget(_genotypeFormats);


    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

    // Set the main layout
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    setLayout( mainLayout );

    mainLayout->addWidget( headerLbl );
    mainLayout->addLayout( fileLayout );
    mainLayout->addWidget( _pathLabel );
    mainLayout->addWidget( groupBox );
    mainLayout->addLayout( formatLayout );
    mainLayout->addWidget( buttonBox );

    setWindowTitle(tr("Import Data File"));
}


void DataImportDialog::slotLoadFile() {
    _filePath = QFileDialog::getOpenFileName(this,
                                             tr("Select Data File"),
                                             QDir::currentPath(),
                                             QString("CSV Files *.csv"));
    if( _filePath.isEmpty() )
        return;

    GENOTYPE_INPUT_FORMAT format;
    switch ( _genotypeFormats->currentIndex() ) {
    case 0:
        format = GENOS_HAPLOID;
        break;
    case 1:
        format = GENOS_DIPLOID_ONE_COLUMN;
        break;
    case 2:
        format = GENOS_DIPLOID_TWO_COLUMN;
        break;
    case 3:
        format = GENOS_TETRAPLOID;
        break;
    }

    FileLoader *loader = new FileLoader( _filePath);
    loader->loadIndividuals(_numStrataEdit->text().toInt(),
                            _numSpatialEdit->text().toInt(),
                            _numExternalEdit->text().toInt(),
                            _numLociEdit->text().toInt(),
                            format );
    if( loader->count()) {
        _theIndividuals = loader->getIndividuals();
    }
    free( loader );


    _pathLabel->setText(QString("<font color='green'>%1</font>").arg(_filePath ));

}
